/**
 * @file contests/cf1702/d/main.cpp
 * @brief
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/7/10 22:03:08
 **/

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
const int C = 30;
int cnt[C];
int del[C];
int main() {
  // High rating and good luck!
  ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  int tc;
  cin >> tc;
  while (tc--) {
    memset(cnt, 0, sizeof(cnt));
    memset(del, 0, sizeof(del));
    string s;
    cin >> s;
    int original = 0;
    for (char c : s) {
      cnt[c - 'a' + 1]++;
      original += c - 'a' + 1;
    }
    int p;
    cin >> p;
    for (int i = 26; i >= 1 && original > p; --i) {
      int needToDel = ((original - p) - 1) / i + 1;
      needToDel = min(needToDel, cnt[i]);
      del[i] = needToDel;
      original -= i * needToDel;
    }
    for (char c : s) {
      if (del[c - 'a' + 1]) {
        del[c - 'a' + 1]--;
      } else {
        cout << c;
      }
    }
    cout << endl;
  }
  return 0;
}
